<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class SignController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function sign_in(Request $request)
    {
        $method = $request->method();
        if($method == 'GET')
        {
            return view('Admin/Login/sign_in');
        }
        if($method == 'POST')
        {
            $res = $request->except('submit');
            $username = $res['username'];
            $password = $res['password'];

//          todo数据验证,密码加密
            //使用邮箱登录
            preg_match('/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i',$username,$match);
            if($match)
            {
                $data = DB::table('admin')->select('email', 'mobile', 'password')->where('email', $username)->get();
                if ($data[0]->password == hash('md5', $password, false)) {
                    return redirect('/');
                } else {
                    return redirect('signIn');
                }
            }


              //使用手机号登录

              preg_match('/^1[3,5,7,8]\d{9}$/',$username,$match);
              if($match)
              {
                  $data = DB::table('admin')->select('email','mobile','password')->where('mobile',$username)->get();
                  if($data[0]->password == hash('md5',$password,false))
                  {
                      return redirect('/');
                  }
                  else
                  {
                      return redirect('signIn');
                  }
              }



            }
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
